package cn.ibobo.lease.admin.service.impl;

import cn.ibobo.lease.admin.dao.UserCouponMapper;
import cn.ibobo.lease.admin.dao.UserDepositMapper;
import cn.ibobo.lease.common.base.BaseMapper;
import cn.ibobo.lease.common.base.BaseServiceImpl;
import cn.ibobo.lease.admin.dao.UserMapper;
import cn.ibobo.lease.admin.service.UserService;
import cn.ibobo.lease.common.dto.UserCouponDTO;
import cn.ibobo.lease.common.dto.UserDTO;
import cn.ibobo.lease.common.dto.UserDepositDTO;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.DateUtil;
import cn.ibobo.lease.common.util.Md5Util;
import cn.ibobo.lease.common.util.UuidUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * 会员信息表业务处理接口
 * @Author lililiang
 * @Date 2018-12-13 18:36:40
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserDTO, String> implements UserService {

    private static final Log LOGGER = LogFactory.getLog(UserServiceImpl.class);

    @Autowired
    private UserMapper mapper;
    @Autowired
    private UserDepositMapper userDepositMapper;
    @Autowired
    private UserCouponMapper userCouponMapper;



    @Override
    protected BaseMapper<UserDTO, String> getMapper() {
        return mapper;
    }

    /**
     * 会员信息表列表
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    public List<UserDTO> list4Page(UserDTO entity, Integer pageNum, Integer... pageSize) {
        return super.list4Page(entity, pageNum, pageSize);
    }

    /**
     * 会员信息表列表（无分页）
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    public List<UserDTO> listByCondition(UserDTO entity) {
        return super.listByCondition(entity);
    }

    /**
     * 查询会员信息表详情信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    public UserDTO queryOneByPk(String id) {
        return super.queryOneByPk(id);
    }

    /**
     * 保存会员信息表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer save(UserDTO entity) {
        //1.对密码进行加密处理
        String passwd = entity.getPasswd();
        if(passwd != null && passwd.length() > 0){
            entity.setPasswd(Md5Util.md5Suffix(passwd));
        }
        String uid = entity.getUid();
        if(uid == null || uid.length() == 0){
            entity.setUid(UuidUtil.getUuid());
            return mapper.add(entity);
        }else{
            return mapper.edit(entity);
        }
    }

    /**
     * 删除会员信息表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer delete(String id) {
        return super.delete(id);
    }

    /**
     * @Description 会员充/退押金
     * @Author lililiang
     * @Date 2019/2/13 19:02
     * @Param [entityDTO]
     * @Return boolean
     */
    @Override
    @Transactional
    public Integer deposit(UserDTO entityDTO){
        //1. 修改用户的押金金额
        if(mapper.deposit(entityDTO) == 0){
            return ResultEnum.FAIL.getCode();
        }
        //2. 增加押金变动记录
        UserDepositDTO depositDTO = new UserDepositDTO();
        depositDTO.setId(UuidUtil.getUuid());
        depositDTO.setUid(entityDTO.getUid());
        depositDTO.setMoney(entityDTO.getDeposit());
        depositDTO.setCreatetm(DateUtil.getNow());
        depositDTO.setType(entityDTO.getType());
        if(userDepositMapper.add(depositDTO) == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultEnum.FAIL.getCode();
        }
        return ResultEnum.SUCCESS.getCode();
    }

    /**
     * @Description 平台赠送点金券
     * @Author lililiang
     * @Date 2019/2/16 16:12
     * @Param [entityDTO]
     * @Return java.lang.Integer
     */
    public Integer coupon(UserCouponDTO entityDTO) {
        String uid = entityDTO.getUid();
        if(uid == null || uid.length() == 0){
            return ResultEnum.FAIL.getCode();
        }
        String[] uids = uid.split(",");
        for(int i=0; i<uids.length; i++){
            UserDTO user = new UserDTO();
            user.setUid(uids[i]);
            user.setCoupon(entityDTO.getMoney());
            user.setType((byte)1);
            if(mapper.coupon(user) <= 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResultEnum.FAIL.getCode();
            }
            entityDTO.setId(UuidUtil.getUuid());
            entityDTO.setStatus(0);
            entityDTO.setUid(uids[i]);
            entityDTO.setCreatetm(DateUtil.getNow());
            if(userCouponMapper.add(entityDTO) <= 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResultEnum.FAIL.getCode();
            }
        }

        return ResultEnum.SUCCESS.getCode();

    }
}
